source ${path}/flow/proc.tcl
source ${path}/flow/pgr/pg_proc.tcl
source ${path}/flow/pgr/pg_psw_conf.tcl
source ${path}/flow/conf.tcl

set PG(M0) M0
set PG(M3) M3

set PG(power)  "VDDC"
set PG(ground) "GNDC"

set PG(direction,H) "horizontal"
set PG(direction,V) "vertical"

set PG(width,M3) 0.024
set PG(pitch,M3) 0.04

proc pr_connect_pg {} {
    global PG
    
    set power_pin  "VDD VPP"
    set ground_pin "VSS VBB"

    globalNetConnect $PG(power) -type pgpin -pin $power_pin -all -override
    globalNetConnect $PG(ground) -type pgpin -pin $ground_pin -all -override
}

proc pr_add_M0 {} {
    global PG

    setSrouteMode -reset
    setSrouteMode -corePinJoinLimit    10
    setSrouteMode -extendNearestTarget true 
    setSrouteMode -treatEndcapAsCore   true
    setSrouteMode -corePinLayer        $PG(M0)

    sroute  -nets "$PG(power) $PG(ground)" \
            -connect { corePin } \
            -layerChangeRange "$PG(M0) $PG(M0)" \
            -allowJogging 0 \
            -allowLayerChange 0 \
            -checkAlignedSecondaryPin 1 \
            -blockPinTarget { nearestTarget } \
            -crossoverViaTopLayer $PG(M0) \
            -crossoverViaBottomLayer $PG(M0) \
            -targetViaTopLayer $PG(M0) \
            -targetViaBottomLayer $PG(M0) \
            -corePinTarget none \
            -uda  rail_m0

}

proc pr_add_M3 { box } {
    global PG

    addStripe \
	    -layer                 $PG(M3) \
	    -nets                  "$PG(power) $PG(ground)"  \
            -start_from            left \
	    -start                 0 \
	    -spacing               $PG(pitch,M3) \
	    -direction             $PG(direction,V) \
            -width                 $PG(width,M3) \
	    -set_to_set_distance   [expr $PG(pitch,M3)*10] \
	    -area                  $box \
	    -uda                   PG_M3
}

proc pr_delete_PG_net {} {
    global PG

    editDelete -net VDDC
    editDelete -net GNDC
}

proc pr_add_M1ToM3 {} {
    global PG PDI
    
    set box [dbget top.fPlan.coreBox]
    set nets "$PG(power) $PG(ground)"

    pgrStripe_m1_to_m3 $PG(power) ${box} 0.0 0.24 $PG(m1_step) $PG(m2_step)
    pgrStripe_m1_to_m3 $PG(ground) ${box} [expr 0.088*2] 0.48 $PG(m1_step) $PG(m2_step)
}


proc pr_add_M4ToM8 {} {
    global PG
    
    pr_add_PG_layer "M4"
    pr_add_PG_layer "M5"
    pr_add_PG_layer "M6"
    pr_add_PG_layer "M7"
    pr_add_PG_layer "M8"

}

proc pr_add_M4ToM6 {} {
    global PG
    pr_add_PG_layer "M4"
    pr_add_PG_layer "M5"
    pr_add_PG_layer "M6"
}

proc pr_add_PG_layer { layer } {
    global PG

    set minWidth   [dbget [dbget head.layers.name ${layer} -p].width]
    set minSpacing [dbget [dbget head.layers.name ${layer} -p].minSpacing]

    set box [dbget top.fplan.box]
    set nets "$PG(power) $PG(ground)"

    if { [string equal ${layer} "M4"] || [string equal ${layer} "M6"] || [string equal ${layer} "M8"] || [string equal ${layer} "M10"]} {
        set dire $PG(direction,H)
    }

    if { [string equal ${layer} "M5"] || [string equal ${layer} "M7"] || [string equal ${layer} "M9"] || [string equal ${layer} "M11"]} {
        set dire $PG(direction,V)
    }

    pr_init_addstripe
    #pr_set_addstripe ${layer} ${nets} 0 [expr ${minSpacing}*2] ${dire} [expr ${minWidth}*4] 1.0 ${box} "PR_PG"
    pr_set_addstripe ${layer} ${nets} 0 0.2 ${dire} 0.2 2.0 ${box} "PR_PG"
}

proc pr_init_addstripe {} {
        setViaGenMode    -reset
        setViaGenMode    -optimize_cross_via  true
        setViaGenMode    -allow_via_expansion false
        setViaGenMode    -keep_existing_via   2

        setAddStripeMode -reset
	setAddStripeMode -ignore_DRC                  true
        setAddStripeMode -ignore_nondefault_domains   true
        setAddStripeMode -use_exact_spacing           true
        setAddStripeMode -remove_floating_stapling    true
        setAddStripeMode -remove_floating_stripe_over_block false
}

proc pr_set_addstripe {layer nets_name start space direction width distance area uda } {       
        addStripe \
            -layer                  $layer \
            -nets                   "VDDC GNDC" \
            -start_from             bottom \
	        -start                  $start \
            -start_offset           0 \
            -spacing                ${space} \
            -direction              ${direction} \
            -width                  $width \
            -set_to_set_distance    $distance \
            -area                   $area \
            -uda                    $uda
}

proc pr_add_PG_VIA {} {
    set fpBox [lindex [dbget top.fplan.box] 0]
    
    editPowerVia -add_vias 1 -area $fpBox -bottom_layer M3 -top_layer M4 -nets "VDDC GNDC"
    editPowerVia -add_vias 1 -area $fpBox -bottom_layer M4 -top_layer M5 -nets "VDDC GNDC"
    editPowerVia -add_vias 1 -area $fpBox -bottom_layer M5 -top_layer M6 -nets "VDDC GNDC"
    editPowerVia -add_vias 1 -area $fpBox -bottom_layer M6 -top_layer M7 -nets "VDDC GNDC"
    editPowerVia -add_vias 1 -area $fpBox -bottom_layer M7 -top_layer M8 -nets "VDDC GNDC"
}

proc pr_add_IP_VIA {} {
    set boxs [dbget selected.box]
    editPowerVia -add_vias 1 -area $boxs -bottom_layer M6 -top_layer M7 -nets "VDDC GNDC"
    deselectAll
}